package com.czl.module_storehouse.adapter;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.czl.base.data.bean.tengyun.LocatListBean;
import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.databinding.ItemInventorySelectChildrenBinding;
import com.czl.module_storehouse.databinding.ItemInventorySelectParentBinding;
import com.czl.module_storehouse.listener.OnExpandableItemListener;
import com.czl.module_storehouse.widget.CommonCounterView;

import java.util.HashMap;
import java.util.List;

/**
 * @author xubin
 */
public class InventorySelectAdapter extends BaseExpandableListAdapter {

    private List<LocatListBean> mData;
    private OnExpandableItemListener<SortBean> mOnExpandableItemListener;

    public InventorySelectAdapter(List<LocatListBean> data) {
        mData = data;
    }

    public List<LocatListBean> getData() {
        return mData;
    }

    @Override
    public int getGroupCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        List<SortBean> sortList = mData.get(groupPosition).getSortList();
        return sortList == null ? 0 : sortList.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mData.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        List<SortBean> sortList = mData.get(groupPosition).getSortList();
        return sortList.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        List<SortBean> sortList = mData.get(groupPosition).getSortList();
        Integer sortId = sortList.get(childPosition).getSortId();
        return sortId == null ? 0 : sortId;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ItemInventorySelectParentBinding binding;
        if (convertView == null) {
            binding = ItemInventorySelectParentBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            convertView = binding.getRoot();
            convertView.setTag(binding);
        } else {
            binding = (ItemInventorySelectParentBinding) convertView.getTag();
        }
        LocatListBean locatListBean = mData.get(groupPosition);
        String text = locatListBean.getStorehouseLocatName() + "(" + locatListBean.getLocationStockNum() + ")";
        binding.tvLoc.setText(text);
        //如果是展开状态
        if (isExpanded) {
            binding.ivDown.setImageDrawable(ContextCompat.getDrawable(parent.getContext(), R.drawable.picture_icon_arrow_down));
        } else {
            binding.ivDown.setImageDrawable(ContextCompat.getDrawable(parent.getContext(), R.drawable.picture_icon_arrow_up));
        }
        return binding.getRoot();
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ItemInventorySelectChildrenBinding binding;
        if (convertView == null) {
            binding = ItemInventorySelectChildrenBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
            convertView = binding.getRoot();
            convertView.setTag(binding);
        } else {
            binding = (ItemInventorySelectChildrenBinding) convertView.getTag();
        }
        SortBean sortBean = mData.get(groupPosition).getSortList().get(childPosition);
        binding.tvTitle.setText(sortBean.getSortName());
        binding.tvNum.setText(sortBean.getInventoryNum() == null ? "--" : String.valueOf(sortBean.getInventoryNum()));
        binding.tvCount.setText("数量：" + sortBean.stockNum());
        binding.tvResultNum.setText(resultString(sortBean));
        binding.tvModel.setText("型号：" + sortBean.getSortModel());
        binding.ivRight.setVisibility(isProduct(sortBean) ? View.VISIBLE : View.GONE);
        binding.tvNum.setVisibility(isProduct(sortBean) ? View.VISIBLE : View.GONE);
        if (isProduct(sortBean)) {
            binding.countView.setVisibility(View.GONE);
            binding.clRoot.setBackgroundResource(R.drawable.ripple_layout_white);
        } else {
            binding.countView.setVisibility(View.VISIBLE);
            binding.clRoot.setBackground(null);
        }
        CommonCounterView counterView = binding.countView;
        counterView.setCount(sortBean.getInventoryNum() == null ? 0 : sortBean.getInventoryNum());
        counterView.setMaxCount(CommonCounterView.MAX_COUNT);
        HashMap<String, Integer> map = new HashMap<>(0);
        map.put("groupPosition", groupPosition);
        map.put("childPosition", childPosition);
        counterView.setTag(map);
        counterView.setTextChangeListener((view, count) -> {
            HashMap<String, Integer> tag = (HashMap<String, Integer>) view.getTag();
            int groupPosition1 = tag.get("groupPosition");
            int childPosition1 = tag.get("childPosition");
            mData.get(groupPosition1).getSortList().get(childPosition1).setInventoryNum(count);
        });
        counterView.setTextChangeListener((view, count) -> sortBean.setInventoryNum(count));
        binding.clRoot.setOnClickListener(v -> {
            if (isProduct(sortBean)) {
                if (mOnExpandableItemListener != null) {
                    mOnExpandableItemListener.onChildItemClick(v, groupPosition, childPosition, sortBean);
                }
            }
        });
        return convertView;
    }

    private String resultString(SortBean sortBean) {
        Integer surplusNum = sortBean.getSurplusNum();
        Integer lossesNum = sortBean.getLossesNum();
        String result = "";
        if (surplusNum == null) {
            surplusNum = 0;
        }
        if (lossesNum == null) {
            lossesNum = 0;
        }
        if (lossesNum > 0) {
            //盘亏
            result += "盘亏" + lossesNum + " ";
        }
        if (surplusNum > 0) {
            //盘盈
            result += "盘盈" + surplusNum;
        }
        if (TextUtils.isEmpty(result)) {
            result = sortBean.getInventoryResultStr();
        }
        return result;
    }

    public boolean isProduct(SortBean sortBean) {
        return sortBean.getProductList() != null && !sortBean.getProductList().isEmpty();
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @SuppressLint("SetTextI18n")
    public void replaceAll(List<LocatListBean> inventorySortInfo, ExpandableListView listView, TextView textView) {
        if (inventorySortInfo == null) {
            return;
        }
        mData = inventorySortInfo;
        notifyDataSetChanged();
        int i = 0;
        int count = 0;
        for (LocatListBean locatListBean : mData) {
            int total = 0;
            List<SortBean> sortList = locatListBean.getSortList();
            if (sortList == null || sortList.isEmpty()) {
                continue;
            }
            count += sortList.size();
            for (SortBean sortBean : sortList) {
                total += sortBean.stockNum();
            }
            locatListBean.setLocationStockNum(total);
            listView.expandGroup(i);
            i++;
        }
        textView.setText("盘点物品（" + count + "）");
    }

    public void setOnExpandableItemListener(OnExpandableItemListener<SortBean> onExpandableItemListener) {
        mOnExpandableItemListener = onExpandableItemListener;
    }
}
